import twitter
import livejournal
import sys
import getopt
import sqlite3
import time

def printUsage():
	print "NAME"
	print "\tSNP - Social Network Parser\n"

	print "SYNOPSIS"
	print "\tpython snp.py [ OPTION ] ...\n"

	print "DESCRIPTION"
	print "\tSearch through social networking sites for\n\tkeywords and rank results based on keywords\n"

	print "\t-a  --all"
	print "\t\tUse all available social networks\n"
	print "\t-c  --commandline"
	print "\t\toutput to command line instead of a file\n"
	print "\t-d  --database= file"
	print "\t\tdatabase file to use/(create if it does not exist)\n"
	print "\t-o  --output= file"
	print "\t\tfile to output results.\n"
	print "\t-h  --help"
	print "\t\tshow this usage information\n"
	print "\t-L"
	print "\t\tuse LiveJournal\n"
	print "\t-r  --rank= file"
	print "\t\tfile holding word/value pairs\n"
	print "\t-t  --tag= file"
	print "\t\tlocation of a file containing search tags\n"
	print "\t-T"
	print "\t\tuse twitter\n"
	print "\t-v  --verbose"
	print "\t\tshow debug and status information during execution\n"



	print "EXAMPLES"
	print "\t python snp.py -t tagfile.txt -d myDatabase.db -a"
	

def main():
	
	try :
		opts, args = getopt.getopt(sys.argv[1:], "r:t:hTLaco:vd:s:", ["commandline", "rank","all","help", "output=", "verbose", "tag=","database="])
	except getopt.GetoptError, err:
		print str(err)
		printUsage()
		sys.exit(2)
	output = None
	verbose = False
	mode = -1
	goliaths = []
	liveJournals = []
	running = True
	secs = 3600
	databaseFile = 'test.db'

	#check command line args
	for operation,value in opts:
		if operation in ("-v", "verbose"):
			verbose = True
		elif operation in ("-h", "--help"):
			printUsage()
			sys.exit()
		elif operation in ("-o", "--output"):
			output = value
		elif operation in ("-d", "--database"):
			databaseFile = value
		elif operation in ("-t", "--tag"):
			f = open(value, 'r')
			searchStrings = f.readlines()
		elif operation in ("-a", "--all"):
			mode = 0
			pass
		else:
			assert False, "Unknown Option"

	#create database connection
	conn = sqlite3.connect(databaseFile)
	c = conn.cursor()
	#check if tables already exist
	c.execute('''select * FROM sqlite_master WHERE type='table' AND name='userdata';''')
	databaseExistanceCheck = 0
	for results in c:
		databaseExistanceCheck += 1
	#if table does not exist, create it
	if databaseExistanceCheck == 0:
		c.execute('create table userdata(name text, TL text, followers int, datetime int, score int, hash text, info text)')

	#initialize Workers/;.
	#create twitter watcher(s)
	for search in searchStrings:
		print "creating goliath"
		goliaths.append(twitter.goliath(search, 500, data = databaseFile))
	#create livejournal watcher(s)
	for search in searchStrings:
		print "creating livejournal Watchers"
		liveJournals.append(livejournal.livejournal_parse(search, database = databaseFile))

	secs = 3600 / 150 * len(searchStrings) + 10

	#enter main loop
	while True:
		#run query on each
		print "Searching"
		for goliath in goliaths:
			print "running goliath"
			goliath.run()
		for live in liveJournals:
			print "running livejournal watcher"
			live.run()
		print "Waiting"
		time.sleep(float(secs))
		print "IN DB:"
		c.execute('select * FROM userdata')
		for result in c:
			print result
	
	c.execute('select * from userdata')
	for result in c:
		print result
	print "Done."
	c.close()





if __name__ == "__main__":
    main()


